﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using mitasoft.Component.Models.Blog;

namespace mitasoft.Web.Core
{
    public class MediaObjectHelper
    {
        public static void SaveMediaObjectFromLiveWriter(MediaObject mediaObject)
        {
            var physicalPath = HttpContext.Current.Server.MapPath("~/" + Path.Combine("Media", mediaObject.Name));
            var directoryPath = new FileInfo(physicalPath).DirectoryName;

            if (File.Exists(physicalPath))
            {
                File.Delete(physicalPath);
            }

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            using (FileStream writer = new FileStream(physicalPath, FileMode.Create))
            {
                writer.Write(mediaObject.Bits, 0, mediaObject.Bits.Length);
            }
        }
    }
}